package servlets;

import entities.User;
import java.io.IOException;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sessions.IUserAccountSession;
import util.BreadCrumbItem;
import util.Script;

/**
 * Trabalho PD - 2014/2015
 *
 * @author a21210380 a21210392
 */
@WebServlet("/createtrip.html")
public class CreateTripServlet extends ServletBase {

    private final static BreadCrumbItem[] BREAD_CRUMB_ITEMS = {
        new BreadCrumbItem("/Trabalho_PD/homepage.html", "Homepage", false),
        new BreadCrumbItem("/Trabalho_PD/mytrips.html", "My trips", false),
        new BreadCrumbItem("/Trabalho_PD/createtrip.html", "Create trip", true)};
    
    private final static String[] EXTRA_STYLES = {"resources/css/jquery.datetimepicker.css"};
    
    private final static Script[] EXTRA_SCRIPTS = {
        new Script("resources/js/webserviceboleias.js", true), 
        new Script("resources/js/jquery.datetimepicker.js", true), 
        new Script("resources/js/customjs/createtrip.js", true)};

    @Inject
    private IUserAccountSession userAccountSession;

    public CreateTripServlet() {
        super("Create trip", BREAD_CRUMB_ITEMS, EXTRA_STYLES, EXTRA_SCRIPTS);
    }

    @Override
    protected void onGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (userAccountSession.isVisitor()) {
            callJsp(JSP_NOT_AUTHORIZED, req, resp);
        }
        User user = userAccountSession.getUser();
        if (user != null) {
            req.setAttribute("cars", user.getVehicles());
            callJsp(JSP_CREATE_TRIP, req, resp);
        } else {
            callJsp(JSP_NOT_AUTHORIZED, req, resp);
        }
    }

    @Override
    protected void onPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (userAccountSession.isVisitor()) {
            callJsp(JSP_NOT_AUTHORIZED, req, resp);
        }
        User user = userAccountSession.getUser();
        if (user != null) {
            req.setAttribute("cars", user.getVehicles());
            callJsp(JSP_CREATE_TRIP, req, resp);
        } else {
            callJsp(JSP_NOT_AUTHORIZED, req, resp);
        }
    }
}
